package com.xqt.now.paysdk;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.wxpay.R;
import com.xqt.now.paysdk.XqtPay;

public class MainActivity extends Activity {

	// 商户秘钥
	private static final String key = "130edc2f0de37c26007c7c2c62838ca3";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				prePayMessage();
				XqtPay.Transit(MainActivity.this);
			}
		});

	}

	// 初始化订单参数
	private void prePayMessage() {
		XqtPay.consumerId = "151503";
		XqtPay.mhtOrderName = "商品名称";
		XqtPay.mhtOrderAmt = "1";
		XqtPay.mhtOrderDetail = "商品描述";
		XqtPay.notifyUrl = "http://localhost:10802/";
		XqtPay.superid = "100000";
		XqtPay.payChannelType = "13";
		XqtPay.mhtOrderNo = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA)
				.format(new Date());
		XqtPay.sign = Sign();
	}

	// 同步回调处理,以异步回调为准
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}
		String respCode = data.getExtras().getString("respCode");
		String respMsg = data.getExtras().getString("respMsg");
		String errorCode = data.getExtras().getString("errorCode");
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("支付结果通知");
		StringBuilder temp = new StringBuilder();
		if (respCode.equals("00")) {
			temp.append("交易状态:成功");
		} else if (respCode.equals("02")) {
			temp.append("交易状态:取消");
		} else if (respCode.equals("01")) {
			temp.append("交易状态:失败").append("\n").append("错误码:")
					.append(errorCode).append("原因:" + respMsg);
		} else if (respCode.equals("03")) {
			temp.append("交易状态:未知").append("\n").append("原因:" + respMsg);
		} else {
			temp.append("respCode=").append(respCode).append("\n")
					.append("respMsg=").append(respMsg);
		}
		builder.setMessage(temp.toString());
		builder.setInverseBackgroundForced(true);
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	// 签名
	private String Sign() {
		String str = "customerid=" + XqtPay.consumerId + "&sdcustomno="
				+ XqtPay.mhtOrderNo + "&orderAmount=" + XqtPay.mhtOrderAmt
				+ key;
		return getMD5(str).toUpperCase();
	}

	public static String getMD5(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());
			return getHashString(digest);

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String getHashString(MessageDigest digest) {
		StringBuilder builder = new StringBuilder();
		for (byte b : digest.digest()) {
			builder.append(Integer.toHexString((b >> 4) & 0xf));
			builder.append(Integer.toHexString(b & 0xf));
		}
		return builder.toString();
	}

}
